﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace Tnelab.Wpf
{
    public class BundledTheme : ResourceDictionary
    {
        private Color? _primaryColor = null;
        private Color? _regionColor = null;
        private Color? _transparentColor = null;
        public Color? PrimaryColor { 
            get {
                return _primaryColor;
            }
            set {
                Application.Current.Resources["LightPrimaryColor"] = LightColor(value.Value,0.26);
                Application.Current.Resources["PrimaryColor"] = value.Value;
                Application.Current.Resources["DarkPrimaryColor"] = value.Value;

                Application.Current.Resources["LightDangerColor"] = HueColor(value.Value, 99);
                Application.Current.Resources["DangerColor"] = HueColor(value.Value, 66);
                Application.Current.Resources["DarkDangerColor"] = HueColor(value.Value, 66);

                Application.Current.Resources["LightWarningColor"] = HueColor(value.Value, 66);
                Application.Current.Resources["WarningColor"] = HueColor(value.Value, 33);
                Application.Current.Resources["DarkWarningColor"] = HueColor(value.Value, 33);

                Application.Current.Resources["LightInfoColor"] = HueColor(value.Value, -66);
                Application.Current.Resources["InfoColor"] = HueColor(value.Value, -33);
                Application.Current.Resources["DarkInfoColor"] = HueColor(value.Value, -33);

                Application.Current.Resources["LightSuccessColor"] = HueColor(value.Value, -99);
                Application.Current.Resources["SuccessColor"] = HueColor(value.Value, -66);
                Application.Current.Resources["DarkSuccessColor"] = HueColor(value.Value, -66);


                Application.Current.Resources["PrimaryTextColor"] = value.Value;
                Application.Current.Resources["SecondaryTextColor"] = LightColor(value.Value, -0.13);
                Application.Current.Resources["ThirdlyTextColor"] = LightColor(value.Value, -0.26);
                Application.Current.Resources["ReverseTextColor"] = LightColor(value.Value, -0.39);
                Application.Current.Resources["TextIconColor"] = value.Value;

                Application.Current.Resources["BorderColor"] = LightColor(value.Value, -0.13);
                Application.Current.Resources["SecondaryBorderColor"] = value.Value;
                _primaryColor = value;
            } 
        }
        public Color? RegionColor {
            get
            {
                return _regionColor;
            }
            set
            {               
                Application.Current.Resources["BackgroundColor"] = LightColor(value.Value, -0.13);
                Application.Current.Resources["RegionColor"] = value.Value;
                Application.Current.Resources["SecondaryRegionColor"] = LightColor(value.Value,0.13);
                Application.Current.Resources["ThirdlyRegionColor"] = LightColor(value.Value, 0.26);
                Application.Current.Resources["TitleColor"] = LightColor(value.Value,-0.13);
                Application.Current.Resources["SecondaryTitleColor"] = HueColor(value.Value,33);
                _regionColor = value;
            }
        }
        public Color? TransparentColor
        {
            get
            {
                return _transparentColor;
            }
            set
            {
                //var blurWindowStyle = Application.Current.Resources["ColoringWindowBase"] as Style;
                //var setters = blurWindowStyle.Setters;
                //foreach (Setter setter in setters)
                //{
                //    if (setter.Property == WindowEx.BlurableProperty)
                //    {
                //        setter.Value = true;
                //        break;
                //    }
                //}
                SetTransparentColor("LightPrimaryColor", value.Value);
                SetTransparentColor("PrimaryColor", value.Value);
                SetTransparentColor("DarkPrimaryColor", value.Value);


                SetTransparentColor("BackgroundColor", value.Value);
                SetTransparentColor("RegionColor", value.Value);
                SetTransparentColor("SecondaryRegionColor", value.Value);
                SetTransparentColor("ThirdlyRegionColor", value.Value);
                SetTransparentColor("TitleColor", value.Value);
                SetTransparentColor("SecondaryTitleColor", value.Value);

                _transparentColor = value;
            }
        }
        /// <summary>
        /// 调整颜色亮度
        /// </summary>
        /// <param name="color"></param>
        /// <param name="addedLightness"></param>
        /// <returns></returns>
        Color LightColor(Color color,double val)
        {
            var hslColor = new HSLColor(color);
            hslColor.Lightness += val;
            hslColor.Lightness=hslColor.Lightness < 0 ? 0 : hslColor.Lightness;
            hslColor.Lightness = hslColor.Lightness > 1 ? 1 : hslColor.Lightness;
            return hslColor.Color;
        }
        /// <summary>
        /// 调整颜色色调
        /// </summary>
        /// <param name="color"></param>
        /// <param name="addedLightness"></param>
        /// <returns></returns>
        Color HueColor(Color color, int val)
        {
            var hslColor = new HSLColor(color);
            hslColor.Hue += val;
            return hslColor.Color;
        }
        /// <summary>
        /// 调整颜色饱和度
        /// </summary>
        /// <param name="color"></param>
        /// <param name="addedLightness"></param>
        /// <returns></returns>
        Color SaturationColor(Color color, double val)
        {
            var hslColor = new HSLColor(color);
            hslColor.Saturation += val;
            hslColor.Saturation = hslColor.Saturation < 0 ? 0 : hslColor.Saturation;
            hslColor.Saturation = hslColor.Saturation > 1 ? 1 : hslColor.Saturation;
            return hslColor.Color;
        }
        void SetTransparentColor(string key,Color val)
        {
            var color = (Color)Application.Current.Resources[key];
            color.A = val.A;
            Application.Current.Resources[key] = color;
        }
    }
}
